<?php
/**
* @file $Id: Preferences.php 528 2007-05-21 19:55:02Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

if($_REQUEST['values'] && $_POST['values'])
{
	if($_REQUEST['tab']=='password')
	{
		if(strtolower($_REQUEST['values']['new'])!=strtolower($_REQUEST['values']['verify']))
			$error = _('Your new passwords did not match.');
		else
		{
			if(User('PROFILE')!=='student')
				$password_RET = DBGet(DBQuery("SELECT PASSWORD FROM USERS WHERE USERNAME='".User('USERNAME')."' AND SYEAR='".UserSyear()."'"));
			else
				$password_RET = DBGet(DBQuery("SELECT PASSWORD FROM STUDENTS WHERE STUDENT_ID='".$_SESSION['STUDENT_ID']."'"));
			if(strtolower($password_RET[1]['PASSWORD'])!=strtolower($_REQUEST['values']['current']))
				$error = _('Your current password was incorrect.');
			else
			{
				if(User('PROFILE')=='student')
					DBQuery("UPDATE STUDENTS SET PASSWORD='".$_REQUEST['values']['new']."' WHERE STUDENT_ID='".$_SESSION['STUDENT_ID']."'");
				else
					DBQuery("UPDATE USERS SET PASSWORD='".$_REQUEST['values']['new']."' WHERE USERNAME='".User('USERNAME')."' AND SYEAR='".UserSyear()."'");
				$note = _('Your new password was saved.');
			}
		}
	}
	else
	{
		$current_RET = DBGet(DBQuery("SELECT TITLE,VALUE,PROGRAM FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM IN ('Preferences','StudentFieldsSearch','StudentFieldsView') "),array(),array('PROGRAM','TITLE'));

		// IF YOU ADD SOMETHING HERE ADD THE SAME TWO LINES TO SystemPreferences.php	
		if($_REQUEST['tab']=='student_listing' && $_REQUEST['values']['Preferences']['SEARCH']!='Y')
			$_REQUEST['values']['Preferences']['SEARCH'] = 'N';
		if($_REQUEST['tab']=='student_listing' && $_REQUEST['values']['Preferences']['DEFAULT_ALL_SCHOOLS']!='Y')
			$_REQUEST['values']['Preferences']['DEFAULT_ALL_SCHOOLS'] = 'N';
		if($_REQUEST['tab']=='student_listing' && $_REQUEST['values']['Preferences']['NAME_FORMAT']!='Y')
			$_REQUEST['values']['Preferences']['NAME_FORMAT'] = 'N';
		if($_REQUEST['tab']=='student_listing' && $_REQUEST['values']['Preferences']['DISPLAY_STUDENT_ID']!='Y')
			$_REQUEST['values']['Preferences']['DISPLAY_STUDENT_ID'] = 'N';
		if($_REQUEST['tab']=='student_listing' && $_REQUEST['values']['Preferences']['DISPLAY_GRADE']!='Y')
			$_REQUEST['values']['Preferences']['DISPLAY_GRADE'] = 'N';

		if($_REQUEST['tab']=='display_options' && $_REQUEST['values']['Preferences']['HIDDEN']!='Y')
			$_REQUEST['values']['Preferences']['HIDDEN'] = 'N';
		// IF YOU ADD SOMETHING HERE ADD THE SAME TWO LINES TO SystemPreferences.php	

		if($_REQUEST['tab']=='display_options' && $_REQUEST['values']['Preferences']['THEME']!=$current_RET['Preferences']['THEME'][1]['VALUE'])
		{
			echo '<script language=JavaScript>';
			echo 'parent.side.location="'.$_SESSION['Side_PHP_SELF'].'?modcat="+parent.side.document.forms[0].modcat.value;';
			echo "parent.help.location='Bottom.php?modcat=Users&modname=$_REQUEST[modname]';";
			echo '</script>';
		}
	
		if($_REQUEST['tab']=='student_fields')
		{
			DBQuery("DELETE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM IN ('StudentFieldsSearch','StudentFieldsView')");

			foreach($_REQUEST['values'] as $program=>$values)
			{
				foreach($values as $name=>$value)
				{
					if(isset($value))
						DBQuery("INSERT INTO PROGRAM_USER_CONFIG (USERNAME,PROGRAM,TITLE,VALUE) values('".User('USERNAME')."','$program','$name','$value')");
				}
			}
		}
		else
		{
			foreach($_REQUEST['values'] as $program=>$values)
			{
				foreach($values as $name=>$value)
				{

					if(!$current_RET[$program][$name] && $value!='')
						DBQuery("INSERT INTO PROGRAM_USER_CONFIG (USERNAME,PROGRAM,TITLE,VALUE) values('".User('USERNAME')."','$program','$name','$value')");
					elseif($value!='')
						DBQuery("UPDATE PROGRAM_USER_CONFIG SET VALUE='$value' WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='$program' AND TITLE='$name'");
					else
						DBQuery("DELETE FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM='$program' AND TITLE='$name'");
				}
			}
		}

		if($_REQUEST['tab']=='display_options' && $_REQUEST['values']['Preferences']['MENU']!=$current_RET['Preferences']['MENU'][1]['VALUE'])
		{
			echo '<script language=JavaScript>';
			echo 'parent.location="index.php?modname='.$_REQUEST['modname'].'"';
			echo '</script>';
		}

		// So Preferences() will get the new values
		unset($_FOCUS['Preferences']);
	}
	unset($_REQUEST['values']);
	unset($_SESSION['_REQUEST_vars']['values']);
	if($_REQUEST['return']=='list')
	{
		header('Location: '.str_replace('&amp;','&',$_SESSION['List_PHP_SELF']));
		exit;
	}
	elseif($_REQUEST['return']=='search')
	{
		header('Location: '.str_replace('&amp;','&',$_SESSION['Search_PHP_SELF']));
		exit;
	}
}

if((!$_REQUEST['return'] || !$_REQUEST['values']) && $_REQUEST['modname']!='School_Setup/SystemPreferences.php')
	DrawHeader(ProgramTitle());

unset($_REQUEST['search_modfunc']);
unset($_SESSION['_REQUEST_vars']['search_modfunc']);

if(!$_REQUEST['modfunc'])
{
	if($_REQUEST['modname']=='School_Setup/SystemPreferences.php')
		$current_RET = DBGet(DBQuery("SELECT TITLE,VALUE,PROGRAM FROM PROGRAM_CONFIG WHERE PROGRAM IN ('Preferences','StudentFieldsSearch','StudentFieldsView') "),array(),array('PROGRAM','TITLE'));
	else
		$current_RET = DBGet(DBQuery("SELECT TITLE,VALUE,PROGRAM FROM PROGRAM_USER_CONFIG WHERE USERNAME='".User('USERNAME')."' AND PROGRAM IN ('Preferences','StudentFieldsSearch','StudentFieldsView') "),array(),array('PROGRAM','TITLE'));
	
	if(!$_REQUEST['tab'])
		$_REQUEST['tab'] = 'display_options';

	if($_REQUEST['tab']=='student_fields' && $_REQUEST['return']=='list')
	{
		$list_PHP_SELF = str_replace('&amp;expanded_view=true','',str_replace('&amp;expanded_view=false','',$_SESSION['List_PHP_SELF']));
		$programs = array($list_PHP_SELF=>_('Simple List'),$list_PHP_SELF . "&expanded_view=true"=>_('Customized List'),'Modules.php?modname=Users/Preferences.php&tab=student_fields&return='.$_REQUEST['return']=>_('Customize Student List'));
		foreach($programs as $modname=>$title)
		{

			if($modname!='Modules.php?modname=Users/Preferences.php&tab=student_fields&return='.$_REQUEST['return'])
			{
				$tabcolor = 'DFDFDF';
				$textcolor = '666666';
			}
			else
			{
				$tabcolor = Preferences('HIGHLIGHT');
				$textcolor = 'FFFFFF';
			}
	
			$header .= '<TD width=10></TD><TD>'.DrawTab($title,$modname,$tabcolor,$textcolor,'_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF')).'</TD>';
		}
		
		echo TabsHeader($header);
	}
	elseif($_REQUEST['tab']=='student_fields' && $_REQUEST['return']=='search')
	{
		$programs = array($_SESSION['Search_PHP_SELF']=>_('Search Screen'),'Modules.php?modname=Users/Preferences.php&tab=student_fields&return='.$_REQUEST['return']=>_('Customize Search Screen'));
		foreach($programs as $modname=>$title)
		{

			if($modname!='Modules.php?modname=Users/Preferences.php&tab=student_fields&return='.$_REQUEST['return'])
			{
				$tabcolor = 'DFDFDF';
				$textcolor = '666666';
			}
			else
			{
				$tabcolor = Preferences('HIGHLIGHT');
				$textcolor = 'FFFFFF';
			}
	
			$header .= '<TD width=10></TD><TD>'.DrawTab($title,$modname,$tabcolor,$textcolor,'_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF')).'</TD>';
		}
		
		echo TabsHeader($header);
	}
	
	if($_REQUEST['return'])
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab]&return=".$_REQUEST['return']."&_FOCUS_PDF=true method=POST>";
	elseif($_REQUEST['modname']!='School_Setup/SystemPreferences.php')
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=$_REQUEST[tab] method=POST>";

	if($_REQUEST['modname']!='School_Setup/SystemPreferences.php')
		DrawHeader('','<INPUT type=submit value='._('Save').'>');
	echo '<BR>';

	if($_REQUEST['modname']=='School_Setup/SystemPreferences.php')
	{
		if(User('PROFILE')=='admin' || User('PROFILE')=='teacher')
			$tabs = array(array('title'=>_('Display Options'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=display_options"),array('title'=>_('Student Listing'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=student_listing"));
		else
			$tabs = array(array('title'=>_('Display Options'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=display_options"));
	}
	else
	{
		if(User('PROFILE')=='admin' || User('PROFILE')=='teacher')
			$tabs = array(array('title'=>_('Display Options'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=display_options"),array('title'=>_('Student Listing'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=student_listing"),array('title'=>_('Password'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=password"),array('title'=>_('Columns in Student Lists'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=student_fields"));
		else
			$tabs = array(array('title'=>_('Display Options'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=display_options"),array('title'=>_('Password'),'link'=>"Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=password"));
	}
	$_FOCUS['selected_tab'] = "Modules.php?modname=$_REQUEST[modname]&amp;system_tab=$_REQUEST[system_tab]&amp;tab=".$_REQUEST['tab'];
	PopTable('header',$tabs);

	echo '<fieldset>';

	if($_REQUEST['tab']=='student_listing')
	{
		echo '<TABLE>';
		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Student Sorting').'</TD><TD><INPUT type=radio name=values[Preferences][SORT] value=Name'.((Preferences('SORT')=='Name')?' CHECKED':'').'>'._('Name').'<BR><INPUT type=radio name=values[Preferences][SORT] value=Grade'.((Preferences('SORT')=='Grade')?' CHECKED':'').'>'._('Grade, Name').'<BR><INPUT type=radio name=values[Preferences][SORT] value=Enrolled'.((Preferences('SORT')=='Enrolled')?' CHECKED':'').'>'._('Enrollment Date, Name').'</TD></TR>';
		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('File Export Type').'</TD><TD><INPUT type=radio name=values[Preferences][DELIMITER] value=Tab'.((Preferences('DELIMITER')=='Tab')?' CHECKED':'').'>'._('Tab-Delimited (Excel)').'<BR><INPUT type=radio name=values[Preferences][DELIMITER] value=CSV'.((Preferences('DELIMITER')=='CSV')?' CHECKED':'').'>'._('CSV').'</TD></TR>';
		$count = 0;
		$tabs_RET = DBGet(DBQuery("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE"));
		foreach($tabs_RET as $tab)
		{
			$table .= '<TD><INPUT type=radio name=values[Preferences][DEFAULT_TAB] value='.$tab['ID'].((Preferences('DEFAULT_TAB')==$tab['ID'])?' CHECKED':'').'>'.$tab['TITLE'].'</TD>';
			$count++;
			if($count%3==0)
				$table .= '</TR><TR>';
		}

		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Default Student Tab').'</font></TD><TD><TABLE><TR>';
		echo $table;
		echo '</TR></TABLE></TD></TR>';


		echo '<TR><TD><BR></TD><TD><BR></TD>';
		echo '<TR><TD></TD><TD><INPUT type=checkbox name=values[Preferences][SEARCH] value=Y'.((Preferences('SEARCH')=='Y')?' CHECKED':'').'>'._('Display student search screen by default').'</TD></TR>';
		if(User('PROFILE')=='admin' || User('PROFILE')=='teacher')
			echo '<TR><TD></TD><TD><INPUT type=checkbox name=values[Preferences][DEFAULT_CUSTOMIZED_LIST] value=Y'.((Preferences('DEFAULT_CUSTOMIZED_LIST')=='Y')?' CHECKED':'').'>'._('Display customized list by default').'</TD></TR>';
		if(User('PROFILE')=='admin')
			echo '<TR><TD></TD><TD><INPUT type=checkbox name=values[Preferences][DEFAULT_ALL_SCHOOLS] value=Y'.((Preferences('DEFAULT_ALL_SCHOOLS')=='Y')?' CHECKED':'').'>'._('Search all schools by default').'</TD></TR>';
		echo '<TR><TD></TD><TD><BR><INPUT type=checkbox name=values[Preferences][NAME_FORMAT] value=Y'.((Preferences('NAME_FORMAT')=='Y')?' CHECKED':'').'>'._('Display student name as Last, First M in student lists').'</TD></TR>';
		echo '<TR><TD></TD><TD><BR><INPUT type=checkbox name=values[Preferences][DISPLAY_STUDENT_ID] value=Y'.((Preferences('DISPLAY_STUDENT_ID')=='Y')?' CHECKED':'').'>'._('Display Student ID in student lists').'</TD></TR>';
		echo '<TR><TD></TD><TD><INPUT type=checkbox name=values[Preferences][DISPLAY_GRADE] value=Y'.((Preferences('DISPLAY_GRADE')=='Y')?' CHECKED':'').'>'._('Display grade level in student lists').'</TD></TR>';
		echo '</TABLE>';
	}
	
	if($_REQUEST['tab']=='display_options')
	{
		echo '<TABLE>';
		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Theme').'</font></TD><TD><TABLE><TR>';
		$count = 0;
		if($handle = opendir($staticpath.'/assets/themes/'))
		{
			while(false !== ($file = readdir($handle)))
			{ 
				if($file != "." && $file != ".." && !in_array($file,$IgnoreFiles))
				{
					echo '<TD><INPUT type=radio name=values[Preferences][THEME] value='.$file.((Preferences('THEME')==$file)?' CHECKED':'').'>'.$file.'</TD>';
					$count++;
					if($count%3==0)
						echo '</TR><TR>';
				}
			}
			closedir($handle); 
		}
		echo '</TR></TABLE></TD></TR>';

		$count = 0;
		if($handle = opendir($staticpath.'/locale/'))
		{
			while(false !== ($file = readdir($handle)))
			{ 
				if($file != "." && $file != ".." && !in_array($file,$IgnoreFiles))
				{
					$table .= '<TD><INPUT type=radio name=values[Preferences][LANGUAGE] value='.$file.((Preferences('LANGUAGE')==$file)?' CHECKED':'').'>'.$file.'</TD>';
					$count++;
					if($count%3==0)
						$table .= '</TR><TR>';
				}
			}
			closedir($handle); 
		}
		if($count>1)
		{
			echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Language').'</font></TD><TD><TABLE><TR>';
			echo $table;
			echo '</TR></TABLE></TD></TR>';
		}

		if(User('PROFILE')=='admin' || User('PROFILE')=='teacher')
			echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Menu Location').'</TD><TD><INPUT type=radio name=values[Preferences][MENU] value=Side'.((Preferences('MENU')=='Side')?' CHECKED':'').'>'._('Side').'<INPUT type=radio name=values[Preferences][MENU] value=Top'.((Preferences('MENU')=='Top')?' CHECKED':'').'>'._('Top').'</TD></TR>';
		//echo '<TR><TD valign=top align=right><font color=gray>'._('Field Label Position').'</TD><TD><INPUT type=radio name=values[Preferences][LABEL_POSITION] value="bottom"'.((Preferences('LABEL_POSITION')=='bottom')?' CHECKED':'').'>Bottom<INPUT type=radio name=values[Preferences][LABEL_POSITION] value="top"'.((Preferences('LABEL_POSITION')=='top')?' CHECKED':'').'>Top</TD></TR>';
		$colors = array('FFCCFF','9999FF','D0D0FF','3399FF','CCFFCC','33FF66','FFFFCC','FFFF66','FFCCCC','CC6666','CCCCCC','999999');
		echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('List Color').'</font></TD><TD><TABLE><TR>';
		foreach($colors as $color)
			echo '<TD bgcolor=#'.$color.'><INPUT type=radio name=values[Preferences][COLOR] value='.$color.((Preferences('COLOR')==$color)?' CHECKED':'').'></TD>';
		echo '</TR></TABLE></TD></TR>';

		$colors = array('330099','3366FF','003333','FF3300','660000','666666','414155');
		echo '<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Highlight Color').'</font></TD><TD><TABLE><TR>';
		foreach($colors as $color)
			echo '<TD bgcolor=#'.$color.'><INPUT type=radio name=values[Preferences][HIGHLIGHT] value='.$color.((Preferences('HIGHLIGHT')==$color)?' CHECKED':'').'></TD>';
		echo '</TR></TABLE></TD></TR>';

		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'><font color=gray>'._('Date Format').'</font></TD><TD>';
		echo '<SELECT name=values[Preferences][WEEKDAY]>';
		$values = array('','D');
		foreach($values as $value)
			echo '<OPTION value="'.$value.'"'.((Preferences('WEEKDAY')==$value)?' SELECTED':'').'>'.date($value).'</OPTION>';
		echo '</SELECT>';
		echo '<SELECT name=values[Preferences][MONTH]>';
		$values = array('F','M','m','n');
		foreach($values as $value)
			echo '<OPTION value='.$value.((Preferences('MONTH')==$value)?' SELECTED':'').'>'.date($value).'</OPTION>';
		echo '</SELECT>';
		echo '<SELECT name=values[Preferences][DAY]>';
		$values = array('d','j','jS');
		foreach($values as $value)
			echo '<OPTION value='.$value.((Preferences('DAY')==$value)?' SELECTED':'').'>'.date($value=='d'?'[0]j':$value).'</OPTION>';
		echo '</SELECT>';
		echo '<SELECT name=values[Preferences][YEAR]>';
		$values = array('Y','y','');
		foreach($values as $value)
			echo '<OPTION value="'.$value.'"'.((Preferences('YEAR')==$value || (!Preferences('YEAR') && !$value))?' SELECTED':'').'>'.date($value).'</OPTION>';
		echo '</SELECT>';
		echo '</TD></TR>';
		echo '<TR><TD valign=top align='.ALIGN_RIGHT.'></TD><TD><INPUT type=radio name=values[Preferences][MONTH_DAY] value="mdy"'.((Preferences('MONTH_DAY')=='mdy')?' CHECKED':'').'>'._('Month Day, Year').'<INPUT type=radio name=values[Preferences][MONTH_DAY] value="dmy"'.((Preferences('MONTH_DAY')=='dmy')?' CHECKED':'').'>'._('Day Month, Year').'</TD></TR>';
		if(User('PROFILE')=='admin' || User('PROFILE')=='teacher')
			echo '<TR><TD></TD><TD><BR><INPUT type=checkbox name=values[Preferences][HIDDEN] value=Y'.((Preferences('HIDDEN')=='Y')?' CHECKED':'').'>'._('Display data using hidden fields').'</TD></TR>';
		echo '</TABLE>';
	}
	
	if($_REQUEST['tab']=='password')
	{
		if($error)
			echo ErrorMessage(array($error));
		if($note)
			echo ErrorMessage(array($note),'note');
		echo '<TABLE><TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Current Password').'</font></TD><TD><INPUT type=password name=values[current]></TD></TR>
						<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('New Password').'</font></TD><TD><INPUT type=password name=values[verify]></TD></TR>
						<TR><TD align='.ALIGN_RIGHT.'><font color=gray>'._('Verify New Password').'</font></TD><TD><INPUT type=password name=values[new]></TD></TR></TABLE>';
	}
	
	if($_REQUEST['tab']=='student_fields')
	{
		$categories_RET = DBGet(DBQuery("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE"),array(),array('ID'));
		$categories_RET[0][1]['TITLE'] = _('Addresses');
		$categories_RET[-1][1]['TITLE'] = _('Contact Information');
		
		$custom_fields_RET = DBGet(DBQuery("SELECT f.CATEGORY_ID,f.ID,f.TITLE,'' AS SEARCH,'' AS DISPLAY FROM CUSTOM_FIELDS f,STUDENT_FIELD_CATEGORIES c WHERE c.ID=f.CATEGORY_ID AND f.TYPE!='holder' AND f.TYPE!='log' ORDER BY c.SORT_ORDER,f.TITLE"),array('CATEGORY_ID'=>'_make','SEARCH'=>'_make','DISPLAY'=>'_make'),array('CATEGORY_ID'));
		$custom_fields_RET[-1] = array(0=>'');
		$custom_fields_RET[0] = array(0=>'');

		for($i=(count($custom_fields_RET[1])-1);$i<=0;$i--)
		{
			$custom_fields_RET[1][($i+2)] = $custom_fields_RET[1][$i];
		}

		$THIS_RET['ID'] = 'STUDENT_ID';
		$custom_fields_RET[1][1] = array('CATEGORY_ID'=>_make(1,'CATEGORY_ID'),'ID'=>'STUDENT_ID','TITLE'=>_('Student ID'),'DISPLAY'=>_make('','DISPLAY'));
		$THIS_RET['ID'] = 'GRADE_ID';
		$custom_fields_RET[1][2] = array('CATEGORY_ID'=>_make(1,'CATEGORY_ID'),'ID'=>'GRADE_ID','TITLE'=>_('Grade'),'DISPLAY'=>_make('','DISPLAY'));

		unset($custom_fields_RET[1][0]);

		$THIS_RET['ID'] = 'CONTACT_INFO';
		$custom_fields_RET[-1][] = array('CATEGORY_ID'=>_make(-1,'CATEGORY_ID'),'ID'=>'CONTACT_INFO','TITLE'=>'<IMG SRC=assets/down_phone_button.gif width=15> '._('Contact Info Rollover'),'DISPLAY'=>_make('','DISPLAY'));
		$THIS_RET['ID'] = 'HOME_PHONE';
		$custom_fields_RET[-1][] = array('CATEGORY_ID'=>_make(-1,'CATEGORY_ID'),'ID'=>'HOME_PHONE','TITLE'=>_('Home Phone Number'),'DISPLAY'=>_make('','DISPLAY'));
		$THIS_RET['ID'] = 'GUARDIANS';
		$custom_fields_RET[-1][] = array('CATEGORY_ID'=>_make(-1,'CATEGORY_ID'),'ID'=>'GUARDIANS','TITLE'=>_('Guardians'),'DISPLAY'=>_make('','DISPLAY'));
		$THIS_RET['ID'] = 'ALL_CONTACTS';
		$custom_fields_RET[-1][] = array('CATEGORY_ID'=>_make(-1,'CATEGORY_ID'),'ID'=>'ALL_CONTACTS','TITLE'=>_('All Contacts'),'DISPLAY'=>_make('','DISPLAY'));

		$custom_fields_RET[0][] = array('CATEGORY_ID'=>_make(0,'CATEGORY_ID'),'ID'=>'ADDRESS','TITLE'=>_('None'),'DISPLAY'=>_makeAddress(''));
		$custom_fields_RET[0][] = array('CATEGORY_ID'=>_make(0,'CATEGORY_ID'),'ID'=>'ADDRESS','TITLE'=>'<IMG SRC=assets/house_button.gif> '._('Residence'),'DISPLAY'=>_makeAddress('RESIDENCE'));
		$custom_fields_RET[0][] = array('CATEGORY_ID'=>_make(0,'CATEGORY_ID'),'ID'=>'ADDRESS','TITLE'=>'<IMG SRC=assets/mailbox_button.gif> '._('Mailing'),'DISPLAY'=>_makeAddress('MAILING'));
		$custom_fields_RET[0][] = array('CATEGORY_ID'=>_make(0,'CATEGORY_ID'),'ID'=>'ADDRESS','TITLE'=>'<IMG SRC=assets/bus_button.gif> '._('Bus Pickup'),'DISPLAY'=>_makeAddress('BUS_PICKUP'));
		$custom_fields_RET[0][] = array('CATEGORY_ID'=>_make(0,'CATEGORY_ID'),'ID'=>'ADDRESS','TITLE'=>'<IMG SRC=assets/bus_button.gif> '._('Bus Dropoff'),'DISPLAY'=>_makeAddress('BUS_DROPOFF'));
		unset($custom_fields_RET[0][0]);
		unset($custom_fields_RET[-1][0]);
		
		$columns = array('CATEGORY_ID'=>'','TITLE'=>_('Field'),'DISPLAY'=>_('Display in Student Lists'));
		ListOutput($custom_fields_RET,$columns,'','',array(),array(array('CATEGORY_ID')));
	}

	if($_REQUEST['modname']!='School_Setup/SystemPreferences.php')
		echo '<BR><CENTER><INPUT type=submit value='._('Save').'></CENTER>';
	echo '</fieldset>';
	PopTable('footer');
	echo '</FORM>';
}

function _make($value,$name='')
{	global $THIS_RET,$categories_RET,$current_RET;

	switch($name)
	{
		case 'CATEGORY_ID':
			return '<B>'.$categories_RET[$value][1]['TITLE'].'</B>';
		break;
		
		case 'SEARCH':
			if($current_RET['StudentFieldsSearch'][$THIS_RET['ID']])
				$checked = ' checked';
			return '<INPUT type=checkbox name=values[StudentFieldsSearch]['.$THIS_RET['ID'].'] value=Y'.$checked.'>';
		break;
		
		case 'DISPLAY':
			if($current_RET['StudentFieldsView'][$THIS_RET['ID']])
				$checked = ' checked';
			return '<INPUT type=checkbox name=values[StudentFieldsView]['.$THIS_RET['ID'].'] value=Y'.$checked.'>';
		break;
	}
}

function _makeAddress($value)
{	global $current_RET;
	
	if($current_RET['StudentFieldsView']['ADDRESS'][1]['VALUE']==$value || (!$current_RET['StudentFieldsView']['ADDRESS'][1]['VALUE'] && $value==''))
		$checked = ' CHECKED';
	return '<INPUT type=radio name=values[StudentFieldsView][ADDRESS] value="'.$value.'"'.$checked.'>';
}
?>